<template>
  <div class="form">
    <div class="header">
      <slot name="header"></slot>
    </div>
    <el-form :inline="true" label-width="200px">
      <el-row>
        <template v-for="item in formItems" :key="item.label">
          <el-col v-bind="colLayout">
            <el-form-item
              v-if="!item.isHidden"
              :label="item.label"
              :style="itemStyle"
            >
              <template v-if="item.type === 'input'">
                <el-input
                  :placeholder="item.placeholder"
                  :modelValue="modelValue[item.fileId]"
                  @update:modelValue="emitfileid($event, item.fileId)"
                />
              </template>
              <template v-else-if="item.type === 'select'">
                <el-select
                  :placeholder="item.placeholder"
                  :modelValue="modelValue[item.fileId]"
                  @update:modelValue="emitfileid($event, item.fileId)"
                >
                  <el-option
                    v-for="option in item.options"
                    :key="option.label"
                    :label="option.label"
                    :value="option.value"
                  />
                </el-select>
              </template>

              <template v-else-if="item.type === 'map'">
                <el-input
                  :placeholder="item.placeholder"
                  :modelValue="modelValue[item.fileId]"
                  @update:modelValue="emitfileid($event, item.fileId)"
                  @click="dialogMapVisible = true"
                />
                <el-dialog
                  v-model="dialogMapVisible"
                  width="600px"
                  title="选择地址"
                >
                  <div>
                    <SyMap
                      :width="500"
                      :height="500"
                      @update:jingwei="emitfileid($event, item.fileId)"
                      :jingwei="modelValue[item.fileId]"
                    ></SyMap>
                  </div>
                  <template #footer>
                    <span class="dialog-footer">
                      <el-button @click="dialogMapVisible = false"
                        >取消</el-button
                      >
                      <el-button
                        type="primary"
                        @click="dialogMapVisible = false"
                      >
                        确定
                      </el-button>
                    </span>
                  </template>
                </el-dialog>
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <div class="footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import SyMap from "@/base-ui/map";
interface IFormProps {
  modelValue?: any;
  formItems?: any[];
  itemStyle?: any;
  colLayout?: any;
}

const props = withDefaults(defineProps<IFormProps>(), {
  modelValue: () => ({}),
  formItems: () => [],
  itemStyle: () => ({ marginTop: "20px" }),
  colLayout: () => ({
    xl: 6, // ≥1920px
    lg: 8, // ≥1200px
    md: 12, // ≥992px
    sm: 24, // ≥768px
    xs: 24, // <768px
  }),
});

const emit = defineEmits(["update:modelValue"]);
const emitfileid = (e: any, fileId: any) => {
  console.log(e);
  emit("update:modelValue", { ...props.modelValue, [fileId]: e }); //有重复属性后面覆盖前面
};

const dialogMapVisible = ref(false);
const onMapBtn = () => {};
</script>

<style lang="less" scoped>
.form {
  border-radius: 10px;
  background-color: white;

  .header {
    padding: 10px;
    text-align: center;
  }
  .footer {
    display: flex;
    justify-content: flex-end;
    padding: 10px;
  }
}
.el-form-item {
  color: #ff00b3;
}
</style>
